package com.media.themovieworld.common.fragment;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.concurrent.TimeoutException;

import com.media.themovieworld.MovieWorldApp;
import com.media.themovieworld.api.ApiRequest;
import com.media.themovieworld.api.RequestModel;
import com.media.themovieworld.model.SoapRequestModel;
import com.media.themovieworld.util.SoapUtils;
import com.media.themovieworld.util.network.HttpMovieTask;
import com.media.themovieworld.util.network.util.JsonUtil;



/**
 * The Class MovieTaskScreen.
 * @author HuanND
 */
public abstract class MovieTaskScreen extends MovieWorldScreen{

	/**
	 * Execute soap task.
	 *
	 * @param <T> the generic type
	 * @param soapModel the soap model
	 * @param isShowProgresss the is show progresss
	 * @param listener the listener
	 * @param returnType the return type
	 */
	public <T> void executeSoapTask(final SoapRequestModel soapModel,boolean isShowProgress,final Class<T> returnType,final MovieTaskListener listener){
		HttpMovieTask<String,T> httpSoapTask = new HttpMovieTask<String, T>() {

			@Override
			public T processInBackground(String... pParams)
					throws SocketTimeoutException, TimeoutException {
				String responds = null;
				try {
					responds = SoapUtils.sendRequest(pParams[0], soapModel.requestContent, soapModel.requestAction, soapModel.soapProperty).toString();
				} catch (Throwable e) {
					e.printStackTrace();
					return null;
				}
				
				return JsonUtil.convertObjectFromJsonString(responds, returnType);
			}

			@Override
			public void onUIUpDate(T result) {
//				listener.onTaskComplete(null);
				if (getActivity() !=null && listener!=null) {
					listener.onTaskComplete(result);
				}
			}
			
		};
		httpSoapTask.run(getActivity(), getContentView(),isShowProgress, soapModel.api);
	}
	
	/**
	 * Execute soap task.
	 *
	 * @param <T> the generic type
	 * @param soapModel the soap model
	 * @param isShowProgresss the is show progresss
	 * @param listener the listener
	 * @param returnType the return type
	 */
	public <T> void executeSoapTask(final RequestModel soapModel,boolean isShowProgress,final Class<T> returnType,final MovieTaskListener listener){
		HttpMovieTask<String,T> httpSoapTask = new HttpMovieTask<String, T>() {

			@Override
			public T processInBackground(String... pParams)
					throws SocketTimeoutException, TimeoutException {
				String responds = null;
				try {
					responds = ApiRequest.sendRequest(soapModel.params,soapModel.api);
				} catch (Throwable e) {
					e.printStackTrace();
					return null;
				}
				
				return JsonUtil.convertObjectFromJsonString(responds, returnType);
			}

			@Override
			public void onUIUpDate(T result) {
//				listener.onTaskComplete(null);
				if (getActivity() !=null && listener!=null) {
					listener.onTaskComplete(result);
				}
			}
			
		};
		httpSoapTask.run(getActivity(), getContentView(),isShowProgress, soapModel.api);
	}
	
	
	public <T> void executeSoapTaskReturnList(final SoapRequestModel soapModel,boolean isShowProgress,final Class<T> returnType,final MovieTaskListener listener){
		HttpMovieTask<String,ArrayList<T>> httpSoapTask = new HttpMovieTask<String, ArrayList<T>>() {

			@Override
			public ArrayList<T> processInBackground(String... pParams)
					throws SocketTimeoutException, TimeoutException {
				String responds = null;
				try {
					responds = SoapUtils.sendRequest(pParams[0], soapModel.requestContent, soapModel.requestAction, soapModel.soapProperty).toString();
				} catch (Throwable e) {
					e.printStackTrace();
					return null;
				}
				
				return JsonUtil.convertArrayListFromJsonString(responds, returnType);
			}

			@Override
			public void onUIUpDate(ArrayList<T> result) {
//				listener.onTaskComplete(null);
				if (getActivity() !=null && listener!=null) {
					listener.onTaskComplete(result);
				}
			}
			
		};
		httpSoapTask.run(MovieWorldApp.getAppContext(), getContentView(),isShowProgress, soapModel.api);
	}
	
	public <T> void executeSoapTaskReturnList(final RequestModel soapModel,boolean isShowProgress,final Class<T> returnType,final MovieTaskListener listener){
		HttpMovieTask<String,ArrayList<T>> httpSoapTask = new HttpMovieTask<String, ArrayList<T>>() {

			@Override
			public ArrayList<T> processInBackground(String... pParams)
					throws SocketTimeoutException, TimeoutException {
				String responds = null;
				try {
					responds = ApiRequest.sendRequest(soapModel.params, soapModel.api);
				} catch (Throwable e) {
					e.printStackTrace();
					return null;
				}
				
				return JsonUtil.convertArrayListFromJsonString(responds, returnType);
			}

			@Override
			public void onUIUpDate(ArrayList<T> result) {
//				listener.onTaskComplete(null);
				if (getActivity() !=null && listener!=null) {
					listener.onTaskComplete(result);
				}
			}
			
		};
		httpSoapTask.run(MovieWorldApp.getAppContext(), getContentView(),isShowProgress, soapModel.api);
	}
	
	/**
	 * The listener interface for receiving movieTask events.
	 * The class that is interested in processing a movieTask
	 * event implements this interface, and the object created
	 * with that class is registered with a component using the
	 * component's <code>addMovieTaskListener<code> method. When
	 * the movieTask event occurs, that object's appropriate
	 * method is invoked.
	 *
	 * @param <T> the generic type
	 * @see MovieTaskEvent
	 */
	public interface MovieTaskListener{
		
		/**
		 * On task complete.
		 *
		 * @param object the object
		 */
		public void onTaskComplete(Object object);
	}

}
